/**
 * Created by lwy on 2017/5/21.
 */
function GetQueryString(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null) return  unescape(r[2]); return null;
}
var shop=document.getElementById('shop');
shop.addEventListener('tap',function(){
    window.location.href="cart.html";
})
var me=document.getElementById('me');
me.addEventListener('tap',function(){
    var token = getItem('token');
    //alert(token);
    if(token!=null){
        window.location.href="personal_v2.html";
    }else{
        window.location.href="login.html";
    }
})
var nearby=document.getElementById('nearby');
nearby.addEventListener('tap',function(){
    window.location.href='shop-apply.html';
})
var index=document.getElementById('index');
index.addEventListener('tap',function(){
    window.location.href='index.html';
});

function getItem(name){
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]); return null;
}
function setItem(name,value){
    var exp = new Date();
    exp.setTime(exp.getTime()+1000*60*60*24*15);
    //document.cookie=name+"="+escape(value)+";";
    document.cookie= name + "="+escape(value)+";expires="+exp.toGMTString() + ";";
}
function removeItem(name){
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getItem(name);
    document.cookie= name + "="+cval+";expires="+exp.toGMTString() + ";";
}

function setCityItem(name,value){
    document.cookie=name+"="+escape(value)+";";
}

function setTimeCookie(name,value){
    var exp = new Date();
    exp.setMinutes(exp.getMinutes() + 30);
    document.cookie= name + "="+escape(value)+";expires="+exp.toGMTString() + ";";
}

///*设置与获取Cookie*/
//var Cookie ={}
//Cookie.write = function(key, value, duration){
//    var d = new Date();
//    d.setTime(d.getTime()+1000*60*60*24*30);
//    document.cookie = key + "=" + encodeURI(value) + "; expires=" + d.toGMTString();
//    };
//Cookie.read = function(key){
//    var arr = document.cookie.match(new RegExp("(^| )"+key+"=([^;]*)(;|$)"));
//    if(arr != null)
//        return decodeURIComponent(arr[2]);
//    return "";
//    };
////定义本地存储对象
//function getItem(key){
//    return window.localStorage? localStorage.getItem(key): Cookie.read(key);
//}
//function setItem(key,val){
//    var _localStorage = window.localStorage;
//    if(!_localStorage){
//        return false;
//    }
//    var testKey = 'test';
//    try{
//        alert('1')
//        localStorage.setItem(key,val);
//        return false;
//    }catch (error){
//        alert('2')
//        Cookie.write(key,val);
//        return true;
//    }
//    //if (window.localStorage) {
//    //    if(typeof localStorage === 'object'){
//    //        alert('1')
//    //        Cookie.write(key,val);
//    //    }else{
//    //        alert('2')
//    //        localStorage.setItem(key,val);
//    //    }
//    //} else {
//    //    alert('3')
//    //    Cookie.write(key,val);
//    //}
//}

function ifCyber(){
    if(window.navigator.onLine==true){
        return true;
    }else{
        return false;
    }
}

//图片验证码
// 刷新图片
function changeImg() {
    var imgSrc = $("#imgObj");
    var src = imgSrc.attr("src");
    //var src = '/imageVerifyC/validateCode.json';
    imgSrc.attr("src", changeUrl(src));
    //console.log(src);
}
//为了使每次生成图片不一致，即不让浏览器读缓存，所以需要加上时间戳
// function changeUrl(url) {
//     var timestamp = (new Date()).valueOf();
//     //console.log(timestamp);
//     console.log('0'+url);
//     var index = url.indexOf("?",url);
//     console.log(index);
//     console.log(url.indexOf("&"));
//     if (index > 0) {
//         url = url.substring(0, url.indexOf(url, "?"));
//         console.log('1'+url);
//     }
//     if ((url.indexOf("&") >= 0)) {
//         url = url + "×tamp=" + timestamp;
//         console.log('2'+url);
//     } else {
//         url = url + "?timestamp=" + timestamp;
//         console.log('3'+url);
//     }
//     return url;
// }

function changeUrl(url) {
    var timestamp = (new Date()).valueOf();
    //console.log(timestamp);
    //console.log('0'+url);
    var index = url.indexOf("?",url);
    //console.log(index);
    //console.log(url.indexOf("&"));
    if (index > 0) {
        url = url.substring(0, url.indexOf("?",url));
        //console.log('1'+url);
    }
    if ((url.indexOf("&") >= 0)) {
        url = url + "×tamp=" + timestamp;
        //console.log('2'+url);
    } else {
        url = url + "?timestamp=" + timestamp;
        //console.log('3'+url);
    }
    return url;
}

// 是微信浏览器
function is_weixn(){
    var ua = navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i)=="micromessenger") {
        return true;
    } else {
        return false;
    }
}